package com.lean.pulltorefresh.list;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;
import com.lean.pulltorefresh.R;
import com.lean.pulltorefresh.list.base.PullToRefreshView;
import com.lean.pulltorefresh.view.PullToRefreshProgressDrawable;
import com.lean.pulltorefresh.view.base.BaseDrawable;

/**
 * Created by Lean·He on 16/7/28.
 */
public class PullToListView extends PullToRefreshView {

    public BaseDrawable baseDrawable;
    public View footerView;
    private View pullToRefreshView;
    private View pullToRefreshViewContainer;
    private ValueAnimator pullAnimator;
    private TextView loadingProgressText;
    private int pullToRefreshHeight;

    public PullToListView(Context context) {
        this(context, null);
    }

    public PullToListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PullToListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void initView() {
        pullToRefreshView = LayoutInflater.from(getContext()).inflate(R.layout.pullto_refresh_head_view, null, false);
        loadingProgressText = (TextView) pullToRefreshView.findViewById(R.id.pull_to_refresh_text);
        pullToRefreshHeight = getResources().getDimensionPixelSize(R.dimen.pullto_refresh_header_view_height);
        pullToRefreshView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 0));
        pullToRefreshViewContainer = pullToRefreshView.findViewById(R.id.ptr_container);
        pullToRefreshView.setPadding(0, -pullToRefreshHeight, 0, 0);
        baseDrawable = (BaseDrawable) pullToRefreshView.findViewById(R.id.loading_progress_drawable);
        footerView = LayoutInflater.from(getContext()).inflate(R.layout.view_loading_more, null, false);
        ((PullToRefreshProgressDrawable) footerView.findViewById(R.id.loading_more_drawable)).start();
        addHeaderView(pullToRefreshView);
        setState(RESET);
    }

    @Override
    protected void updateHeaderView(int paddingTop) {
        pullToRefreshView.setPadding(0, paddingTop, 0, 0);
        float process = (paddingTop + pullToRefreshHeight) * 1.0f / pullToRefreshHeight;
        baseDrawable.setProgress(process);
    }

    protected void animationToRefreshPosition(int toPosition) {
        pullAnimator = ValueAnimator.ofFloat(pullToRefreshView.getPaddingTop(), toPosition + getFirstVisibleViewTop());
        pullAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                pullToRefreshView.setPadding(0, (int) value, 0, 0);
                if (-1 * pullToRefreshHeight == value) {
                    baseDrawable.stop();
                }
            }
        });
        pullAnimator.setInterpolator(new DecelerateInterpolator());
        pullAnimator.start();
    }


    @Override
    protected void onReleaseToRefresh() {
        loadingProgressText.setText(getResources().getString(R.string.refresh_release_label));
    }

    @Override
    protected void onRefreshing() {
        if (baseDrawable != null) {
            baseDrawable.start();
        }
        animationToRefreshPosition(pullToRefreshViewContainer.getMeasuredHeight() - pullToRefreshHeight);
        loadingProgressText.setText(getResources().getString(R.string.refresh_refreshing_label));
    }

    @Override
    protected void onPullToRefresh() {
        loadingProgressText.setText(getResources().getString(R.string.refresh_pull_label));
    }

    @Override
    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        super.setOnLoadMoreListener(onLoadMoreListener);
        addFooterView(footerView);
    }

    @Override
    protected void onReset() {
        animationToRefreshPosition(-1 * pullToRefreshHeight);
    }

    @Override
    protected View getHeaderView() {
        return pullToRefreshView;
    }

    @Override
    protected View getFooterView() {
        return footerView;
    }

    @Override
    protected int isReadyToRefreshSize() {
        return pullToRefreshHeight;
    }
}
